import Vue from 'vue'
import axios from 'axios'

// const ADMIN_URL = 'http://api.hxgmw.com/admin'
// const ADMIN_URL = 'http://192.168.18.01/admin'
const ADMIN_URL = 'http://127.0.0.1:3000'


const sendAjax = (url, method, param, success, error) => {
	axios({
		method,
		url,
		baseURL: url.startsWith('http') ? '' : (ADMIN_URL),
		headers: {
			'Accept': 'application/json;chartset=utf-8',
			// 'Client-Platform': 'pc',
			// 'Client-Version': ''
		},
		params: method === 'get' ? param : '',
		data: method !== 'get' ? param : ''
	})
		.then(response => {
			console.log('HTTP 结束:')
			console.log(response)
			const resCode = response.data.code
			if (resCode == 200) {
				success && success(response)
			}
			else {
				console.error('HTTP 接口 报错: ')
				console.error(response.data)
				error && error(response)
			}
		})
		.catch(errorMsg => {
			console.error('HTTP 异常')
			console.error(errorMsg)
		})

}

const startSystem = async () => {
	const http = (url, method, param, success, error) => {
		sendAjax(url, method, param, success, error)
	}
	Vue.prototype.http = http
}


export {
  sendAjax,
  startSystem
}